<?php

use swap\visitor;
use swap\lazy_checker;
use swap\check_failed;

class language_controller extends swap\controller {
    
    public static $layout = 'my';
    
    public static function before_filters() {
        $id = g_int('id', 0);
        
        return array(
            'resume_filter' => array(
                '*' => array('id' => $id),
            ),
        );
    }
    
    public static function before_run() {
        self::role('user', 'redirect_to', 'my-site/login');
    }
    
    public static function index_action() {
        $id = visitor::get_value('id');
        self::set('id', $id);
        
        // 已有的语言能力
        $languages = language_model::get_existed_language($id);
        self::set('languages', $languages);
        
        self::show_page();
    }
    
    public static function new_action() {
        // none
    }
    
    public static function do_new_action() {
        // 拦截
        self::method('post');
        self::csrf('user');
        
        try {
            $id = visitor::get_value('id');
            
            // 校验
            $props = self::check_form(p(), 'new');
            
            // 检查
            resume_ext_service::can_set_resume_ext($id, 'language');
            
            // 执行
            $language_id = language_model::add($props);
            resume_ext_service::set_resume_ext($id, 'language', $language_id);
            
            // 正常
            self::json_result(true, '添加成功');
            
        } catch (resume_ext_except $se) {
            // 异常
            self::json_result(false, $se->get_msg(), 0, $se->get_prop_name());
        } catch (check_failed $e) {
            $msg = $e->get_reasons();
            self::json_result(false, current($msg), 0, key($msg));
        }
    }
    
    public static function edit_action() {
        // 校验language_id
        $language = self::check_language_id();
        self::helper_set('language', $language);
        
        self::show_page();
    }
    
    public static function do_edit_action() {
        // 拦截
        self::csrf('user');
        self::method('post');

        try {
            // 校验language_id
            $language = self::check_language_id();

            // 校验
            $props = self::check_form(p(), 'edit');

            // 更新
            language_model::set_by_id($props, $language->id);

            // 成功
            self::json_result(true, '编辑成功');
        } catch (check_failed $e) {
            // 失败
            $msg = $e->get_reasons();
            self::json_result(false, current($msg), 0, key($msg));
        }
    }
    
    public static function del_action() {
        // none
    }
    
    public static function do_del_action() {
        // 拦截
        self::csrf('user');
        
        // 校验language_id
        $language = self::check_language_id();
        
        // 删除
        resume_ext_service::del_resume_ext(visitor::get_value('id'), 'language', $language->id);
        language_model::del_by_id($language->id);
        
        // 成功
        self::json_result(true);
    }
    
    protected static function check_form($post, $operation) {
        $checker = new lazy_checker($post);
        $checker->check_model_rules('language', $operation);
        extract($checker->get_all());
        // 执行
        $id = visitor::get_value('id');
        $props = array(
            'resume_id' => $id,
            'category' => $category,
            'write_skill' => $write_skill,
            'read_skill' => $read_skill,
        );
        return $props;
    }
    
    protected static function check_language_id() {
        $language_id = g_int('language_id', 0);
        $language = language_model::get_by_id($language_id);
        if ($language === null) {
            self::forward_404('非法请求');
        }
        return $language;
    }
    
}